package com.adt.studioms.web.controller.user;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.adt.studioms.StudiomsApplication;
import com.adt.studioms.api.TaskAPI;
import com.adt.studioms.om.Task;
import com.asao.baseware.core.security.ITrustedPassport;
import com.asao.baseware.core.util.CommonValidation;
import com.asao.baseware.core.web.controller.EntityCommand;
import com.asao.baseware.core.web.controller.EntityFormController;

public class TaskController extends EntityFormController<TaskCommand> {
    TaskAPI taskAPI;

    class TaskValidator implements Validator {
        
        public boolean supports(Class clazz) {
            return clazz.equals(TaskCommand.class);
        }

        public void validate(Object target, Errors errors) {
            TaskCommand cmd = (TaskCommand) target;
            
            if (StringUtils.isEmpty(cmd.getCode())) {
                errors.rejectValue("code", "task.codeIsRequired", "Code is required");
            } else if (!CommonValidation.isLengthInRange(cmd.getCode(), 1, 10)) {
                errors.rejectValue("code", "task.codeLengthIsNotInRange", "Code length is not in range (1-10)");
            } else if (cmd.getAction()==EntityCommand.CREATE || cmd.getAction()==EntityCommand.UPDATE) {
                Task task = taskAPI.getByCode(cmd.getCode());
                if (task!=null && task.getId()!=cmd.getId())
                    errors.rejectValue("code", "task.codeIsExisted", "Code is existed");
            }
            
            if (StringUtils.isEmpty(cmd.getName())) {
                errors.rejectValue("name", "task.nameIsRequired", "Name is required");
            } else if (!CommonValidation.isLengthInRange(cmd.getName(), 1, 40)) {
                errors.rejectValue("name", "task.nameLengthIsNotInRange", "Name length is not in range (1-40)");
            }
        }
    }
    
    public TaskController() {
        super();
        taskAPI = StudiomsApplication.instance.getApiRegistry().lookupAPI(ITrustedPassport.instance, TaskAPI.class);
        
        setCommandClass(TaskCommand.class);
        setValidator(new TaskValidator());
    }

    protected void publishEntity(long id, TaskCommand command) {
        Task task = taskAPI.get(id);
        if (task!=null) {
            command.setId(id);
            command.setName(task.getName());
            command.setCode(task.getCode());
        }
    }

    protected void createEntity(TaskCommand command, BindException errors) {
        Task task = taskAPI.createTask(command.getCode(), command.getName());
        command.setId(task.getId());
    }

    protected void updateEntity(TaskCommand command, BindException errors) {
        Task task = taskAPI.get(command.getId());
        if (task!=null) {
            task.setCode(command.getCode());
            task.setName(command.getName());
            
            taskAPI.update(task);
        }
    }

    protected void deleteEntity(TaskCommand command, BindException errors) {
        taskAPI.delete(command.getId());
    }
}

class TaskCommand extends EntityCommand {
    String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}
